<template>
	<view class="purchaseAppointAdd">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :statusBar="true" :title="navBarTitle" fixed="true" />
		<view class="purchaseAppointAdd_top"></view>
		<view class="purchaseAppointAdd_main">
			<dil-grad-form
				:stepOptions="stepOptions"
				:stepActive="stepActive"
				:formList="formList"
				:formValue="formValue"
				@next="next"
				@previous="previous"
				@cancelForm="cancelForm"
				@submitGradForm="submitGradForm"
			></dil-grad-form>
		</view>
		<view class="purchaseAppointAdd_floot"></view>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		return {
			navBarTitle: '',
			stepOptions: [{ title: '主体信息' }, { title: '物流信息' }, { title: '其他信息' }],
			stepActive: 0,
			formList: [
				{
					formId: 510,
					formValue: {}
				},
				{
					formId: 522,
					formValue: {}
				},
				{
					formId: 410,
					formValue: {}
				}
			],
			formValue: {},
			submitForm: {
				requirementNo: '',
				requirementId: '',
				businessPersonnelId: '',
				organizationId: '',
				businessCompanyId: '',
				orderId: '',
				lineId: '',
				controlMode: '',
				printStampList: 0,
				nuclearLoadControl: '',
				dueTime: '',
				businessType: '',
				unloadFee: '',
				remark: '',
				sbddList: null,
				fhdd: '',
				shippingCompanyId: '',
				receivingCompanyId: '',
				receiveCompanyId: '',
				supplierCompanyId: '',
				children: [
					{
						orderChildId: null,
						requireNumber: '',
						requireWeight: '',
						transTruckNumber: '',
						controlRate: 100,
						shippingPointId: null,
						receivingPointId: null,
						printStampList: '',
						materialId: null,
						requirementChildId: null
					}
				]
			}
		};
	},
	computed: {
		...mapState('purchase', ['purchaseOrderItem'])
	},
	created() {
		let dateObj = new Date();
		// 设置为当年最后一天
		dateObj.setFullYear(dateObj.getFullYear(), 11, 31); // 月份范围是 0-11，所以11代表12月
		// 调整到该天的最后一刻（23:59:59.999）
		dateObj.setHours(23);
		dateObj.setMinutes(59);
		dateObj.setSeconds(59);
		dateObj.setMilliseconds(999);
		this.formValue.dueTime = dateObj.getTime();
	},
	onShow() {},
	onLoad(option) {
		this.navBarTitle = this.purchaseOrderItem.navBarTitle || '新增收货通知单';
		let map = JSON.parse(JSON.stringify(this.purchaseOrderItem));
		map.transportType = this.purchaseOrderItem.transMode;
		map.requireNumber = this.purchaseOrderItem.materialNumber;
		map.supplierCompanyId = this.purchaseOrderItem.shippingCompanyId;
		map.receiveCompanyName = this.purchaseOrderItem.receiveCompanyName;
		map.receiveCompanyId = this.purchaseOrderItem.receivingCompanyId;
		map.organizationId = this.purchaseOrderItem.rootCompanyId;
		if (this.purchaseOrderItem.sbddList && typeof this.purchaseOrderItem.sbddList == 'string') {
			map.sbddList = this.purchaseOrderItem.sbddList.split(',');
		} else if (this.purchaseOrderItem.sbddList && typeof this.purchaseOrderItem.sbddList == 'object') {
			map.sbddList = this.purchaseOrderItem.sbddList;
		} else {
			map.sbddList = [];
		}
		this.formValue = map;
	},
	methods: {
		//以下是vuex;
		...mapMutations('purchase', ['updatePurchaseOrderItem']),
		...mapMutations('purchase', ['updatePurchaseAppointItem']),
		back() {
			// #ifdef APP-PLUS
			uni.navigateBack();
			// #endif

			// #ifdef H5
			this.$router.back();
			// #endif
		},
		//步骤，上一步
		previous(index, form) {
			this.formValue = form;
			this.stepActive = index;
		},
		next(index, form) {
			this.formValue = form;
			this.stepActive = index;
		},
		//取消事件
		cancelForm(form) {
			this.formValue = form;
			this.back();
			uni.showToast({
				title: '用户取消操作',
				icon: 'error',
				duration: 2000
			});
		},
		//提交事件
		async submitGradForm(form) {
			console.log(form, 'form');
			form.serialNumber = form.requirementNo;
			let url = '/api/v1/ams/purchaseRequirementAdd';
			if (form.navBarFlag == 'update') {
				url = '/api/v1/ams/purchaseRequirementUpdate';
			} else {
				url = '/api/v1/ams/purchaseRequirementAdd';
			}
			this.formValue = form;
			let map = {};
			map.children = [];
			map = { ...this.formValue };
			// Object.keys(this.submitForm).forEach((e) => {
			// 	map[e] = form[e] || this.submitForm[e];
			// });
			// Object.keys(this.submitForm.children[0]).forEach((e) => {
			// 	map.children[0][e] = form[e] || this.submitForm.children[0][e];
			// });
			map.serialNumber = form.requirementNo;
			map.children = [{ ...form }];
			const res = await this.$myRequest({
				url: url,
				data: map
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					this.updatePurchaseAppointItem({});
					this.back();
				}, 2100);
			} else {
				uni.showToast({
					title: res.data.message,
					icon: 'error',
					duration: 2000
				});
			}
		}
	}
};
</script>

<style></style>
